<template>
    <div class="mainDiv">
        <div class="rowHead">
                    <el-button type="success" icon="plus" @click="add" v-qx="permission.add">新增产品</el-button>
                    <el-select v-model="page.zt" class="ml" @change="selstatus" v-qx="permission.status">
                        <el-option v-for="s in status" :label="s.txt" :value="s.val" :key="s.val">
                        </el-option>
                    </el-select>
                    <el-input style="width: 200px" placeholder="搜索产品名称、产品编号" @keyup.enter.native="searchList" v-model="page.search">
                        <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="searchList"></i>
                    </el-input>
                </div>
                <el-table :data="list" :stripe="true" height="100%">
                    <el-table-column prop="id" label="ID" width="80"></el-table-column>
                    <el-table-column prop="name" label="产品名称"></el-table-column>
                    <el-table-column prop="number" label="产品编号"></el-table-column>
                    <el-table-column prop="price" label="标准价格">
                        <template scope="scope">{{scope.row.price+'元'}}</template>
                    </el-table-column>
                    <el-table-column prop="tdtype" label="销售提成">
                        <template scope="scope">
                            <el-tag color="#edaff1" v-if="scope.row.tdtype==1">{{Number(scope.row.percent*100).toFixed(0)+'%'}}</el-tag>
                            <el-tag color="#f3a6bb" v-else>{{scope.row.fixed+'元'}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="khtdtype" label="耗卡提成">
                        <template scope="scope">
                            <el-tag color="#edaff1" v-if="scope.row.khtdtype==1">{{Number(scope.row.khpercent*100).toFixed(0)+'%'}}</el-tag>
                            <el-tag color="#f3a6bb" v-else>{{scope.row.khfixed+'元'}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="bname" label="所属品牌"></el-table-column>
                    <el-table-column prop="cname" label="所属分类"></el-table-column>
                    <el-table-column prop="remarks" label="产品描述" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="nums" label="库存数量"></el-table-column>
                    <el-table-column prop="isdisabled" label="状态" width="60">
                        <template scope="scope">
                            <el-tag :type="scope.row.isdisabled?'danger':'success'">{{scope.row.isdisabled?'弃用':'正常'}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="addtime" label="添加日期" :show-overflow-tooltip="true">
                        <template scope="scope">
                            {{time(scope.row.addtime)}}
                        </template>
                    </el-table-column>
                    <el-table-column label="产品操作" width="250">
                        <template scope="scope">
                            <el-button-group v-if="!scope.row.isdisabled">
                                <el-tooltip content="编辑产品" placement="left" :open-delay="800" v-qx="permission.edit">
                                    <el-button type="warning" icon="edit" size="mini" @click="edit(scope.row)">编辑</el-button>
                                </el-tooltip>
                                <el-tooltip content="停用当前产品" placement="top" :open-delay="800" v-qx="permission.unstop">
                                    <el-button type="danger" icon="h-jinyong" size="mini" @click="stop(scope.row.id,1)">停用</el-button>
                                </el-tooltip>
                            </el-button-group>
                            <el-tooltip content="重新打开当前产品" placement="left" :open-delay="800" v-else v-qx="permission.unstop">
                                <el-button type="success" icon="h-qiyong2" size="mini" @click="stop(scope.row.id,0)">重新打开</el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="block center bg-light">
                    <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="page.pages" :page-sizes="[15, 30, 50, 100]" :page-size="page.limits" layout="total, sizes, prev, pager, next, jumper" :total="total">
                    </el-pagination>
                </div>
    </div>
</template>
<script>
import C from '#/src/common/common.js'
export default {
    name: 'product',
    data () {
        return {
            list: [],
            total: 0,
            page: {
                pages: 1,
                limits: 15,
                search: '',
                zt: 0,
            },
            status: [{
                txt: '正常',
                val: 0
            }, {
                txt: '停用',
                val: 1
            }],
            permission: {
                add: 'goodscategory_add',
                status: 'goodscategory_status',
                edit: 'goodscategory_edit',
                unstop: 'goodscategory_unstop',
            }
        }
    },
    mounted () {
        this.getList()
    },
    methods: {
        add: async function () {
            let res = await C.openBrowserWindow('/editGoods', {}, {
                transparent: true,
                frame: false,
                width: 650,
                height: 450,
                useSpa: true,
                spaModel: 'organization'
            })
            if (res) {
                C.success(res.data)
                this.getList()
            }
        },
        edit: async function (row) {
            let res = await C.openBrowserWindow('/editGoods', { row: row }, {
                transparent: true,
                frame: false,
                width: 650,
                height: 450,
                useSpa: true,
                spaModel: 'organization'
            })
            console.log(res)
            if (res) {
                C.success(res.data)
                this.getList()
            }
        },
        stop: async function (id, st) {
            let that = this
            if (st) {
                C.confirm('停用机构产品, 是否继续?', async () => {
                    let res = await C.ajax('/organization/goods/goodsstatus', { id: id, st: st }, 'post')
                    if (res) {
                        C.success(res.data)
                        that.getList()
                    }
                })
            } else {
                let res = await C.ajax('/organization/goods/goodsstatus', { id: id, st: st }, 'post')
                if (res) {
                    C.success(res.data)
                    that.getList()
                }
            }
        },
        selstatus: function () {
            this.getList()
        },
        refresh: function () {
            window.location.reload()
        },
        sizeChange: function (val) {
            this.page.limits = val
            this.getList()
        },
        currentChange: function (val) {
            this.page.pages = val
            this.getList()
        },
        searchList: function () {
            this.getList()
        },
        getList: async function () {
            let that = this
            let data = await C.ajax('/organization/goods/productlist', this.page, 'post')
            if (data) {
                that.list = data.data
                that.total = data.count
            }
        },
        time: function (unix) {
            return C.times(unix)
        }
    }
}
</script>
<style>
.mb20 {
  margin-bottom: 20px;
}
</style>
